package com.iyue.util;

import java.util.Random;

import com.iyue.SpringHelper;
import com.iyue.framework.cache.IMemcachedCache;
import com.iyue.user.helper.UserFunctions;

/**
 * 《验证码》实现类
 * 
 * @author 黄海清
 * @version $Id: ValidCode.java,v 1.1 2010/10/14 17:07:14 fresh Exp $
 */
public class ValidCode {

	/**
	 * 验证码
	 */
	public static final String M_VALID_CODE = "com.iyue.util.ValidCode_";
	/**
	 * 随机Key验证码
	 */
	public static final String M_VALID_CODE_KEY = "K_";

	public static String vCode(int size, String session) {
		String vcode = getValidCode(size);
		setValidCode(session, vcode);

		return vcode;
	}

	/**
	 * 获取验证码
	 * 
	 * @return
	 */
	public static String getValidCode() {
		return getValidCode(4);
	}

	/**
	 * 获取验证码
	 * 
	 * @param size
	 * @return
	 */
	public static String getValidCode(int size) {
		String[] number = new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9" };
		Random rnd = new Random();
		int p = number.length - 1;

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < size; i++) {
			int j = rnd.nextInt(p) + 1;
			sb.append(number[j]);
		}

		return sb.toString();
	}

	/**
	 * 获取随机Key
	 * 
	 * @param size
	 * @return
	 */
	public static String getKey(int size) {
		String[] number = new String[] { "1", "2", "3", "4", "5", "6", "7",
				"8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i",
				"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
				"v", "w", "x", "y", "z", "A", "B", "C", "Q", "H", "_" };

		Random rnd = new Random();
		int p = number.length - 1;

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < size; i++) {
			int j = rnd.nextInt(p) + 1;
			sb.append(number[j]);
		}

		return sb.toString();
	}

	/**
	 * 判断验证码是否有效
	 * 
	 * @param session
	 * @param vcode
	 * @return
	 */
	public static boolean isValid(String session, String vcode) {
		if (session == null || "".equals(session)) {
			return false;
		}

		if (vcode == null || "".equals(vcode)) {
			return false;
		}

		String mVcode = getValidCode(session);
		delValidCode(session);
		if (mVcode == null || "".equals(mVcode)) {
			return false;
		}

		if (vcode.equals(mVcode)) {
			return true;
		}

		return false;
	}

	/** 
	 * 从缓存中获得验证码
	 * @param session
	 * @return
	 */
	public static String getValidCode(String session) {
		if (session == null || "".equals(session)) {
			return null;
		}

		String key = M_VALID_CODE + session;
		IMemcachedCache cache = (IMemcachedCache) SpringHelper
				.getBean("FastMemcachedCache");

		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				return (String) obj;
			} else {
				cache.delete(key);
			}
		}

		return null;
	}

	/**
	 * 设置验证码
	 * 
	 * @param session
	 * @param vcode
	 */
	public static void setValidCode(String session, String vcode) {
		String key = M_VALID_CODE + session;
		IMemcachedCache cache = (IMemcachedCache) SpringHelper
				.getBean("FastMemcachedCache");
		cache.add(key, vcode, UserFunctions.MINUTE * 10);
	}

	/**
	 * 删除验证码缓存
	 * 
	 * @param session
	 */
	public static void delValidCode(String session) {
		String key = M_VALID_CODE + session;
		IMemcachedCache cache = (IMemcachedCache) SpringHelper
				.getBean("FastMemcachedCache");
		cache.delete(key);
	}

	/**
	 * 生成随机Key和验证码
	 * 
	 * @param codeSize
	 * @param keySize
	 * @return Key
	 */
	public static String setCodeByKey(int codeSize, int keySize) {
		String session = M_VALID_CODE_KEY + getKey(keySize);
		String vcode = getValidCode(codeSize);
		setValidCode(session, vcode);

		return session;
	}
}
